﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Web;
using System.Net;
using System.IO;
using System.Security.Cryptography.X509Certificates;
using OpenNETCF.Security.Cryptography;
using System.Security.Cryptography;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
using FFWinMobile.Properties;
namespace FFWinMobile
{

    public partial class frmMain : Form
    {
        
        public frmMain()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pnlFeeds.Height = (this.Height - 41);
            if (GF.Giris)
            {                
                ReadHomeFeeds();                
            }
            else
            {
                frmLogin frm = new frmLogin();
               DialogResult dr = frm.ShowDialog();
               System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
               if (dr == DialogResult.OK)
               {
                   this.Show();
                   ReadHomeFeeds();
               }
               else
               {
                   Application.Exit();
               }
            }

        }
       
        private void ReadHomeFeeds()
        {
           
            ReadHomeFeeds(null, 0);
            
        }
        private void ReadHomeFeeds(string id, int curPos)
        {
            System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.WaitCursor;
            pnlFeeds.Controls.Clear();
           string response;
           if (id == null)
               response = FF.ReadHomeFeed(curPos);
           else
               response = FF.ReadHomeFeed(FF.ReadFeedWith.WithCommentsAndLikes, curPos);
           JObject j = JObject.Parse(response);
           JArray entries = (JArray)j["entries"];
           int x = 3, y = 3;
            Rectangle rect = pnlFeeds.ClientRectangle;
            rect.Width -= 20;
               
           foreach (JObject entry in entries)
           {
               JToken from = entry["from"];
               LinkLabel user = new LinkLabel();
               user.Location = new Point(x, y);
               user.Font = new Font(FontFamily.GenericSansSerif, 10f, FontStyle.Bold);
               user.ForeColor = Color.Black;
               user.Tag =  from["id"].Value<string>();
               user.Text = from["name"].Value<string>();
               user.Width = rect.Width;
               user.Height = CFMeasureString.MeasureString(user, user.Text, rect).Height;
               user.Click += new EventHandler(user_Click);
               y += 1;
               y += user.Size.Height;
               pnlFeeds.Controls.Add(user);

               JToken body = entry["rawBody"];
               JToken url = entry["rawLink"];
               LinkLabel linkLabel = new LinkLabel();
               linkLabel.Font = new Font(FontFamily.GenericSansSerif, 9f, FontStyle.Regular);
               
               linkLabel.Location = new Point(x,y);               
               linkLabel.Tag = entry["id"].Value<string>();
               linkLabel.Text = body.Value<string>();
               linkLabel.Height = CFMeasureString.MeasureString(linkLabel, linkLabel.Text, rect).Height;
               linkLabel.Click += new EventHandler(linkLabel_Click);
               pnlFeeds.Controls.Add(linkLabel);
               linkLabel.Width = rect.Width;
               y += linkLabel.Size.Height;
               y += 2;

               if (id != entry["id"].Value<string>())
               {
                   int com = 0, like = 0;

                   if (entry["comments"] != null)
                       com = entry["comments"][0]["num"] == null ? entry["comments"].Count() : entry["comments"][0]["num"].Value<int>();
                   if (entry["likes"] != null)
                       like = entry["likes"][0]["num"] == null ? entry["likes"].Count() : entry["likes"][0]["num"].Value<int>();// entry["likes"][0]["num"].Value<int>();

                   if (com != 0 || like != 0)
                   {
                       LinkLabel lblCommentsAndLinks = new LinkLabel();
                       lblCommentsAndLinks.Text = string.Format(Properties.Resources.COMMENT_LINK_SUMMARY, com, like);
                       lblCommentsAndLinks.Location = new Point(x, y);
                       lblCommentsAndLinks.Font = new Font(FontFamily.GenericSansSerif, 8f, FontStyle.Italic);
                       lblCommentsAndLinks.Width = rect.Width;
                       lblCommentsAndLinks.Height = CFMeasureString.MeasureString(lblCommentsAndLinks, lblCommentsAndLinks.Text, rect).Height;
                       lblCommentsAndLinks.Click += new EventHandler(lblCommentsAndLinks_Click);

                       lblCommentsAndLinks.Tag = entry["id"].Value<string>() + "|" + curPos;
                       lblCommentsAndLinks.ForeColor = Color.Gray;
                       y += 1;
                       y += lblCommentsAndLinks.Size.Height;
                       pnlFeeds.Controls.Add(lblCommentsAndLinks);
                   }
               }
               else if (id == entry["id"].Value<string>())
               {
                   JArray comments = (JArray)entry["comments"];
                   if (comments != null)
                   {
                       JToken comment;
                       comment = comments[0];
                       int nameY = y, nameX = x;
                       nameX = 20;
                       while (comment != null)
                       {

                           if (comment["id"] != null)
                           {

                               PictureBox pb = new PictureBox();
                               pb.Image = Properties.Resources.comment1;
                               pb.Size = Properties.Resources.comment1.Size;
                               pb.Location = new Point(x, nameY);
                               pnlFeeds.Controls.Add(pb);

                               JToken commentFrom = comment["from"];
                               Label lblComment = new Label();
                               lblComment.Text = string.Format("{0}", comment["rawBody"].Value<string>());
                               lblComment.Location = new Point(nameX, nameY);
                               lblComment.Font = new Font(FontFamily.GenericSerif, 8f, FontStyle.Regular);
                               lblComment.Width = rect.Width;
                               lblComment.Height = CFMeasureString.MeasureString(lblComment, lblComment.Text, rect).Height;

                               nameY += lblComment.Size.Height + 1;
                               pnlFeeds.Controls.Add(lblComment);

                               LinkLabel lblCommentUser = new LinkLabel();
                               lblCommentUser.Text = commentFrom["name"].Value<string>();
                               lblCommentUser.Tag = commentFrom["id"].Value<string>();
                               lblCommentUser.Click += new EventHandler(lblComment_Click);
                               lblCommentUser.Width = rect.Width - 30;
                               lblCommentUser.TextAlign = ContentAlignment.TopRight;
                               lblCommentUser.Location = new Point(nameX, nameY);
                               pnlFeeds.Controls.Add(lblCommentUser);
                               nameY += lblCommentUser.Size.Height + 1;

                           }
                           else
                           {
                               LinkLabel lblCommentsAndLinks = new LinkLabel();
                               lblCommentsAndLinks.Text = comment["body"].Value<string>();
                               lblCommentsAndLinks.Location = new Point(nameX, nameY);
                               lblCommentsAndLinks.Font = new Font(FontFamily.GenericSansSerif, 8f, FontStyle.Italic);
                               lblCommentsAndLinks.Width = rect.Width;
                               lblCommentsAndLinks.Height = CFMeasureString.MeasureString(lblCommentsAndLinks, lblCommentsAndLinks.Text, rect).Height;
                               nameY += lblCommentsAndLinks.Size.Height + 1;
                               pnlFeeds.Controls.Add(lblCommentsAndLinks);

                           }

                           comment = comment.Next;
                       }
                       y = (nameY + 1);
                   }
                   
                   JArray likes = (JArray)entry["likes"];
                   if (likes != null)
                   {
                       JToken like;
                       like = likes[0];
                       int nameY = y, nameX = x;
                       nameX = 20;
                       Label lbl = new Label();
                       List<string> sb = new List<string>();
                       while (like != null)
                       {
                           if (like["from"] != null)
                           {
                               JToken likeFrom = like["from"];
                               sb.Add(likeFrom["name"].Value<string>());
                           }
                           else
                               sb.Add(like["body"].Value<string>());
                           like = like.Next;
                       }
                       string veriler = string.Join(", ", sb.ToArray()) + " " + Properties.Resources.LIKED;
                       lbl.Text = veriler;
                       int height = CFMeasureString.MeasureString(lbl, lbl.Text, rect).Height;
                       // y += nameY + 2;
                       PictureBox pbLike = new PictureBox();
                       pbLike.Image = Properties.Resources.smile1;
                       pbLike.Size = Properties.Resources.smile1.Size;
                       pbLike.Location = new Point(x, nameY);
                       pnlFeeds.Controls.Add(pbLike);
                       lbl.Font = new Font(FontFamily.GenericSerif, 8f, FontStyle.Regular);
                       lbl.Width = rect.Width;
                       lbl.Height = height;
                       lbl.Location = new Point(nameX, nameY);
                       pnlFeeds.Controls.Add(lbl);
                      
                       y = (nameY + height+ 3);
                   }
                   
                   
               }
               if (GF.ShowPics)
               {
                   if (entry["thumbnails"] != null)
                   {
                       foreach (JToken pic in entry["thumbnails"])
                       {
                           PictureBox pb = new PictureBox();
                           Image i = GF.DownloadImage(pic["url"].Value<string>());
                           pb.Image = i;
                           
                           int picX = i.Width;
                           if (picX > (pnlFeeds.Width - 13))
                               picX = pnlFeeds.Width - 13;

                           pb.Width = picX;
                           pb.Height = i.Height;
                           pb.SizeMode = PictureBoxSizeMode.StretchImage;
                           
                           pb.Tag = pic["link"].Value<string>();
                           pb.Click += new EventHandler(pb_Click);
                           pb.Location = new Point(x, y);
                           y += (pb.Height + 3);
                           pnlFeeds.Controls.Add(pb);
                       }
                   }
               }
             
           }

           y += 4;
           LinkLabel lblPrevious = new LinkLabel();
           lblPrevious.Text = Properties.Resources.PREVIOUS;
           lblPrevious.Height = CFMeasureString.MeasureString(lblPrevious, lblPrevious.Text, rect).Height;
           lblPrevious.Tag = (curPos - GF.PageRecordCount) >= 0 ? (curPos - GF.PageRecordCount) : 0;
           lblPrevious.Enabled = (curPos - GF.PageRecordCount) >= 0 ? true : false;
           lblPrevious.Click += new EventHandler(lblPager_Click);
           lblPrevious.Width = (rect.Width / 2);
           lblPrevious.Location = new Point(x, y);
           pnlFeeds.Controls.Add(lblPrevious);
           pnlFeeds.Controls.Add(lblPrevious);
           x += lblPrevious.Width;

           LinkLabel lblNext = new LinkLabel();
           lblNext.Text = Properties.Resources.NEXT;
           lblNext.Height = CFMeasureString.MeasureString(lblNext, lblNext.Text, rect).Height;
           lblNext.TextAlign = ContentAlignment.TopRight;
           int count = FF.ReadHomeFeedCount();
           lblNext.Tag = count > (curPos + GF.PageRecordCount) ? (curPos + GF.PageRecordCount) : 0;
           lblNext.Enabled = count > (curPos + GF.PageRecordCount) ? true : false;
           lblNext.Click += new EventHandler(lblPager_Click);
           lblNext.Width = (rect.Width / 2);
           lblNext.Location = new Point(x, y);
           pnlFeeds.Controls.Add(lblNext);

           System.Windows.Forms.Cursor.Current = System.Windows.Forms.Cursors.Default;
        }

        void pb_Click(object sender, EventArgs e)
        {
            PictureBox pb = (PictureBox)sender;
            ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe",pb.Tag.ToString());            
            Process.Start(startInfo);           
        }

        void linkLabel_Click(object sender, EventArgs e)
        {
            LinkLabel lbl = (LinkLabel)sender;
            frmEntry frm = new frmEntry(lbl.Tag.ToString());
            frm.Show();
        }

        void lblPager_Click(object sender, EventArgs e)
        {
            LinkLabel lbl = (LinkLabel)sender;
            ReadHomeFeeds(null,  (int)lbl.Tag);
        }

        void lblComment_Click(object sender, EventArgs e)
        {
            LinkLabel lbl = (LinkLabel)sender;
            frmUserForm frm = new frmUserForm(lbl.Tag.ToString());
            frm.Show();
        }  

        void lblCommentsAndLinks_Click(object sender, EventArgs e)
        {
            LinkLabel link = (LinkLabel)sender;
            if (link.Tag != null)
            {
                string[] tags = link.Tag.ToString().Split('|');
                ReadHomeFeeds(tags[0], Convert.ToInt32(tags[1]));
            }
        }

        void user_Click(object sender, EventArgs e)
        {
            LinkLabel lbl = (LinkLabel)sender;
            frmUserForm frm = new frmUserForm(lbl.Tag.ToString());
            frm.Show();         
            
          
        }

  

        private void menuItem1_Click_1(object sender, EventArgs e)
        {
            ReadHomeFeeds();
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            frmUserForm frm = new frmUserForm("me");
            frm.Show();
        }

        private void menuItem5_Click(object sender, EventArgs e)
        {
            frmFeed frm = new frmFeed();
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                frmUserForm frmUser = new frmUserForm("me");
                frmUser.Show();
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
         
        }

        private void miSettings_Click(object sender, EventArgs e)
        {
            frmSettings frm = new frmSettings();
            frm.ShowDialog();
        }

        private void menuItem6_Click(object sender, EventArgs e)
        {
            frmAbout frm = new frmAbout();
            frm.Show();
        }

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {

        }

        private void pbFF_Click(object sender, EventArgs e)
        {

        }

 

       

       

    }



    
    
}